﻿(function (S, J) {
    S.namespace("FGT.core", true);
    FGT.core.pageObj = {
        create: function (pageObj) {
            /// <summary>创建类的函数</summary>
            /// <param name="pageObj" type="object">用对象表示的类</param>
            /// <returns type="object">类的实例，已经执行了类的构造函数</returns>
            var args = J.makeArray(arguments);
            args.shift();
            var tempPageObj = function (params) {
                var self = this;
                self.initialize.apply(self, params);
                self.initializeDOM.apply(self, params);
                self.initializeEvent.apply(self, params);
                self.pageLoad.apply(self, params);
            };
            pageObj.initialize = pageObj.initialize || J.noop;
            pageObj.initializeDOM = pageObj.initializeDOM || J.noop;
            pageObj.initializeEvent = pageObj.initializeEvent || J.noop;
            pageObj.pageLoad = pageObj.pageLoad || J.noop;
            pageObj.dispose = pageObj.dispose || J.noop;
            tempPageObj.prototype = pageObj;
            var result = new tempPageObj(args);
            return result;
        }
    };

    FGT.core.constant = {
        jsonResultCode: {
            SUCCESS: 0,
            ERROR: 1
        }
    };

    FGT.core.GetRealLat = function (lat, key) {
        var latOffsetMeters = parseInt((key / 1000));
        var offsetLat = latOffsetMeters / 100000.0;
        return lat - offsetLat;
    };

    FGT.core.GetRealLng = function (lng, key) {
        var lngOffsetMeters = parseInt((key % 1000));
        var offsetLng = lngOffsetMeters / 100000.0;
        return lng - offsetLng;
    };

    FGT.globalObj = {};

    FGT.alert = function (message) {    //统一的提示框
        alert(message);
    }
    S.namespace("func", true);
    S.namespace("FGT.logic.wp", true);
    S.FGT = FGT;

    J.ajaxSetup({
        timeout: 30000,
        datatype: "json"
    });

    S.ajax.setupConfig({
        timeout: 30000,
        dataType: "json"
    });

    //---------------------------jQuery Extentions------------------------------------------------------------------------------

    /// example: "my name is #{name}".template({name:"HuLingfei"}); // output: my name is HuLingfei
    ////////////////////////////////////////// String /////////////////////////////////
    J.extend(String.prototype, {
        template: function (object) {
            var regex = /#{(.*?)}/g;
            return this.replace(regex, function (match, subMatch, index, source) {
                return object[subMatch] || "";
            });
        }
    });

    /// example: "Hello world!".beginWith("Hello"); // output: true
    ////////////////////////////////////////// String /////////////////////////////////
    J.extend(String.prototype, {
        beginWith: function (subStr) {
            if (this.indexOf(subStr) === 0) {
                return true;
            }
            return false;
        }
    });


    /// example: this.jCheckInTime.click(this.onCheckInTimeClick.proxy(this));
    ////////////////////////////////////////// Function /////////////////////////////////
    J.extend(Function.prototype, {
        proxy: function (context) {
            return J.proxy(this, context);
        }
    });

    /// example: this.jCheckInTime.click(this.onCheckInTimeClick.proxy(this));
    ////////////////////////////////////////// Function /////////////////////////////////
    J.extend(Array.prototype, {
        contains: function (item) {
            for (i = 0; i < this.length; ++i) {
                if (this[i] === item) {
                    return true;
                }
            }
            return false;
        }
    });

})(KISSY, jQuery);KISSY.add("fgt/core/ajax/loading", function (S) {
    function Loading(block, config) {
        var self = this;
        if (S.isString(block)) {
            block = S.all(block);
        }

        if (!block) {
            S.log("请配置正确的block ID.", "error");
            return;
        }

        self._init(block, config || {});
    }

    S.augment(Loading, {
        _init: function (block, config) {
            var self = this;

            self.block = block;
            self.config = S.merge(
            {
                refresh: false,
                loadingHtml: "<div class='loading' style='width: 100%; height: 200px;'><div class='text'><span>加载中...</span></div></div>",
                loadFailureHtml: ""
            }, config);
        },

        load: function () {
            var self = this,
                J = jQuery,
                block = self.block,
                config = self.config,
                refresh = config.refresh,
                url = config.url;

            if ((refresh && !block.hasClass("loading")) || (!block.hasClass("loading") && !block.hasClass("loaded"))) {
                var resultCode = FGT.core.constant.jsonResultCode;
                if (url) {
                    J.ajax({
                        url: url,
                        type: "GET",
                        cache: false,
                        beforeSend: function (jqXHR, settings) {
                            var b = block;
                            b.empty();
                            b.append(config.loadingHtml);
                            b.addClass("loading");
                        },
                        success: function (data, textStatus, jqXHR) {
                            var b = block;
                            if (data.Result === resultCode.SUCCESS) {
                                b.empty();
                                b.append(data.Content);
                                b.addClass("loaded");
                            }
                            else {
                                b.empty();
                                b.append(config.loadFailureHtml);
                            }
                            b.removeClass("loading");
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            var b = block;
                            b.empty();
                            b.append(config.loadFailureHtml);
                            b.removeClass("loading");
                        }
                    });
                }
            }
        }
    });

    return Loading;
});KISSY.add("fgt/core/ajax", function (S, Loading) {

    S.namespace("FGT.core.ajax", true);

    S.mix(S.FGT.core.ajax, {
        Loading: Loading
    });
    return S.FGT.core.ajax;

}, { requires: ["./ajax/loading"] });